package com.sl.au.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.Query;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.ArchiveReportInfo;
import com.sl.au.entity.EventRegister;
import com.sl.au.entity.EventReport;
import com.sl.au.entity.EventTask;
import com.sl.au.repository.ArchiveReportInfoRepository;
import com.sl.au.repository.EventReportRepository;
import com.sl.au.repository.EventTaskRepository;
import com.sl.au.service.ArchiveReportInfoService;

@Service
@Transactional
public class ArchiveReportInfoServiceImpl extends BaseServiceImpl implements ArchiveReportInfoService {

	@Autowired
	private ArchiveReportInfoRepository archiveReportInfoRepository;
	@Autowired
	private EventReportRepository eventReportRepository;
	@Autowired
	private EventTaskRepository eventTaskRepository;
	@Resource
	private JdbcTemplate jdbcTemplate;

	@Override
	public ArchiveReportInfoRepository getRepository() {
		return this.archiveReportInfoRepository;
	}

	@Override
	public void saveMany(EventReport report, ArchiveReportInfo archiveReportInfo, EventTask reportTask) {
		eventReportRepository.save(report);

		archiveReportInfo.setReport(report);
		archiveReportInfoRepository.save(archiveReportInfo);
		// task
		reportTask.setEventId(report.getId());
		reportTask.setEventTaskMemo(report.getReportMemo());
		reportTask.setEventTaskState(report.getReportEventVariable());
		reportTask.setEventTaskTime(new Date());
		reportTask.setEventTaskUser(archiveReportInfo.getArchiver());
		reportTask.setEventTaskOperate("归档事件源");
		eventTaskRepository.save(reportTask);
	}

	@Override
	public List<ArchiveReportInfo> initArchivedPageByKey(String key, int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return archiveReportInfoRepository.initArchivedPageByKey(key, pageable).getContent();
	}

	@Override
	public List<ArchiveReportInfo> initArchivedPageNoKey(int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return archiveReportInfoRepository.initArchivedPageNoKey(pageable).getContent();

	}

	@Override
	public int getArchivedCount(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		if (!StringUtils.hasText(key)) {
			return archiveReportInfoRepository.initArchivedPageNoKey(pageable).getContent().size();
		}
		return archiveReportInfoRepository.initArchivedPageByKey(key, pageable).getContent().size();
	}

	@Override
	public List<EventRegister> showDetailRegisters(String reportId, int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return archiveReportInfoRepository.showDetailRegisters(reportId, pageable).getContent();
	}

	@Override
	public int showDetailRegistersCount(String reportId) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return archiveReportInfoRepository.showDetailRegisters(reportId, pageable).getContent().size();
	}

	@Override
	public List<Map<String, Object>> initArchivedPageByKeyAndRole(int start, int length, String roleId, String key, String orderName, String dir){
		
		//  @Query("select a from ArchiveReportInfo a where a.archiver.name like %?1% or a.content like %?1% or a.memo like %?1%")
		
		String SQL1 = "select  a.*, "
				+ " u.name as 'archiveUserName', "
				+ " e.reportMemo, "
				+ " e.reportEventType, "
				+ " e.realReportEventType "
				+ " from sl_wcsi_archive_report a "
				+ " left join sl_au_user u on u.id = a.archiveUserId "
				+ " left join sl_wcsi_EventReport e on e.id = a.reportId "
				+ " where 1 = 1 ";
		
		if(StringUtils.hasText(key)){
			SQL1+=" and (a.content like '%"+key+"%' or a.memo like '%"+key+"%' or u.name like '%"+key+"%') ";	
		}
		
		
		String SQL = " select sq.* from (select e2r.* from sl_wcsi_e2r e2r where e2r.roleId = '"+ roleId +"' ) er left join ( " + SQL1 + " ) sq on er.eventTypeId = sq.realReportEventType where sq.id is not null ";
		
		SQL += " order by sq." + orderName + " " + ( StringUtils.hasText(dir) ? dir : "" );
		SQL += " limit "+ (int)(Math.floor(start / length) * length) + " , " +  (int)((Math.floor(start / length) + 1) * length);
		
		List<Map<String, Object>> resultList = this.jdbcTemplate.queryForList(SQL);
		
		return resultList;
	}
	
	@Override
	public int getArchivedCountByRole(String roleId, String key) {
		String SQL1 = "select  a.*, "
				+ " u.name as 'archiveUserName', "
				+ " e.reportMemo, "
				+ " e.reportEventType, "
				+ " e.realReportEventType "
				+ " from sl_wcsi_archive_report a "
				+ " left join sl_au_user u on u.id = a.archiveUserId "
				+ " left join sl_wcsi_EventReport e on e.id = a.reportId "
				+ " where 1 = 1 ";
		
		if(StringUtils.hasText(key)){
			SQL1+=" and (a.content like '%"+key+"%' or a.memo like '%"+key+"%' or u.name like '%"+key+"%') ";	
		}
		
		String SQL = " select count(1) from (select e2r.* from sl_wcsi_e2r e2r where e2r.roleId = '"+ roleId +"' ) er left join ( " + SQL1 + " ) sq on er.eventTypeId = sq.realReportEventType where sq.id is not null ";
		
		int count = 0;
		count = this.jdbcTemplate.queryForObject(SQL, Integer.class);
		
		return count;
	}
	
}
